<?php
namespace app\backend\controllers;
use app\backend\common\AdminBaseController;
use app\backend\models\ArticleClassModel;
use app\backend\models\ArticleModel;
use lib\helper\error;
use lib\Tool\Tools;

class ArticleClassController extends AdminBaseController{

    public function actionList(){
        $ArticleClassModel = new ArticleClassModel();
        $data = $ArticleClassModel->getArticleClass();
        $this->assign('data',['class_list'=>$data]);
        return $this->display('class/list');
    }

    public function actionAdd(){
        $post = Tools::My()->request()->post();
        if (!empty($post)){
            $code = ArticleClassModel::find()->insert([
                'class_name'=>$post['class_name'],
                'class_depth'=>$post['class_depth'],
                'father_class_id'=>$post['father_class_id'],
                'is_del'=>0
            ]);
            if ($code > 0){
                error::_plainError(['message'=>'添加成功']);
            }
            error::_plainError(['message'=>'添加失败']);
        }
        return $this->display('class/add');
    }

    public function actionEdit(){
        $post = Tools::My()->request()->post();
        $get = Tools::My()->request()->get();
        if (!empty($post) && !empty($get)){
            $code = ArticleClassModel::find()->where([['=','ac_id',$get['ac_id']]])->update([
                'class_name'=>$post['class_name'],
                'class_depth'=>$post['class_depth'],
                'father_class_id'=>$post['father_class_id'],
                'is_del'=>0
            ]);
            if ($code > 0){
                error::_plainError(['message'=>'修改成功']);
            }
            error::_plainError(['message'=>'修改失败']);
        }
        $data = ArticleClassModel::find()->where([['=', 'ac_id', $get['ac_id']]])->one();
        $this->assign('class_info', $data[0]);
        return $this->display('class/edit');
    }

    public function actionDelOrOn()
    {
        $get = Tools::My()->request()->get();
        $code = ArticleClassModel::find()->where([['=', 'ac_id', $get['ac_id']]])->update(['is_del' => $get['is_del']]);
        if ($code > 0) {
            error::_plainError(['message' => '修改成功']);
        }
        error::_plainError(['message' => '修改失败']);
    }

}